package com.aliyun.messagemgt.util;

import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName: PlaceHolderUtils
 * @Description:
 * @Author: wugongzi
 * @Date: 2021/9/15 16:52
 * @Version: 1.0
 */
@Slf4j
public class PlaceHolderUtils {

    public static String parse(String content, Map<String,String> data){
        String pattern = "\\$\\{(.+?)\\}";
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(content);
        StringBuffer sb = new StringBuffer();
        while (m.find())
        {
            String key = m.group(1);
            String value = data.get(key);
            m.appendReplacement(sb, value == null ? "" : value);
        }
        m.appendTail(sb);
        log.info("content parse:{}",sb);
        return sb.toString();
    }
}
